package cn.stonemind.smu.common.util;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution {

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> numsMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int twoTarget = target - nums[i];
            if (numsMap.containsKey(twoTarget) && numsMap.get(twoTarget) != i) {
                return new int[]{numsMap.get(twoTarget), i};
            }
            numsMap.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    public static int reverse1(int x) {
        if (x == Integer.MIN_VALUE) {
            return 0;
        }
        boolean negativeFlag = false;
        if (x < 0) {
            negativeFlag = true;
        }
        System.out.println(Math.abs(x));
        String reverseX = new StringBuffer(String.valueOf(Math.abs(x))).reverse().toString();
        if (Long.parseLong(reverseX) > Integer.MAX_VALUE) {
            return 0;
        }
        if (negativeFlag) {
            return 0 - Integer.parseInt(reverseX) < Integer.MIN_VALUE ? 0 : 0 - Integer.parseInt(reverseX);
        }
        return Integer.parseInt(reverseX);
    }

    public static int reverse(int x) {
        long res = 0L;
        while (x != 0) {
            int tmp = x % 10;
//            System.out.println(tmp);
            res = res * 10 + tmp;
            if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) {
                return 0;
            }
//            System.out.println(res);

            x = x / 10;
        }
        return (int) res;
    }

    public static boolean isPalindrome1(int x) {
        int tmp = 0;
        if (x < 0) {
            return false;
        }
        String str = String.valueOf(x);
        int len = str.length() - 1;
        while (len > 1) {
            int lastNum = x % 10;
            int firstNum = (int) (x / Math.pow(10, len));
            System.out.println((int) Math.pow(10, len));
            System.out.println(firstNum);
            System.out.println(lastNum);
            if (lastNum != firstNum) {
                return false;
            }
            x = x - firstNum * ((int) Math.pow(10, len) + 1);
            len -= 2;
        }
        return true;
    }

    public static boolean isPalindrome(int x) {
        if (x < 0 || (x % 10 == 0 && x != 0)) {
            return false;
        }
        int revertedNum = 0;
        while( x > revertedNum) {
            revertedNum = revertedNum * 10 + x % 10;
            x /= 10;
        }
        return x == revertedNum || x == revertedNum / 10;
    }

    public static void main(String[] args) {
        // 求一个数组中有没有两个数之和为target， 如果有的话返回两个数的索引
//        int[] nums = {2, 7, 9 , 12};
//        int target = 11;
//        System.out.println(String.valueOf(twoSum(nums, target)));
        // revere

        // 反转数字
//        int num = 14748634;
//        System.out.println(num);
//        System.out.println(reverse(num));


        // 回文数
//        int num = 121;
//        System.out.println(isPalindrome(num));
//
//        System.out.println(11 + 11 % 10);
    }
}
